package services

/*
//25	switchSprinkling	//	启停喷淋 0测试 1启动 2停止
//26	solenoidValveDelay	//	电磁阀延时
//27	sprayIntervalTime	//	点喷间隔时间
//28	fanControl 	//	风扇启动控制 0测试 1启动 2停止
//29	lightControl 	//	灯光启停控制 0测试 1启动 2停止
//30	controllerMode 	//	控制器模式    0  喷淋控制器   1 雨林控制器
//31	solenoidValveMode	//	电磁阀模式
//32	addLightTimePeriod	//	添加灯光时间段
//33	deleteLightTimePeriod2	//	灯光时间段2删除
//34	manual	//	手动
//35	secondCurrentThreshold	//	第二电流阀值
//36	parameterId	//	//参数编号
//37	parameterValue		//参数数值
*/
// 设备控制规则
var DeviceControlDataMap = map[string]DataRule{
	"switchSprinkling":       {StartAddr: 25, Length: 1},
	"solenoidValveDelay":     {StartAddr: 26, Length: 1},
	"sprayIntervalTime":      {StartAddr: 27, Length: 1},
	"fanControl":             {StartAddr: 28, Length: 1},
	"lightControl":           {StartAddr: 29, Length: 1},
	"controllerMode":         {StartAddr: 30, Length: 1},
	"solenoidValveMode":      {StartAddr: 31, Length: 1},
	"addLightTimePeriod":     {StartAddr: 32, Length: 1},
	"deleteLightTimePeriod2": {StartAddr: 33, Length: 1},
	"manual":                 {StartAddr: 34, Length: 1},
	"secondCurrentThreshold": {StartAddr: 35, Length: 1},
	"parameterId":            {StartAddr: 36, Length: 1},
	"parameterValue":         {StartAddr: 37, Length: 1},
}

// 设备控制Params
type DeviceControlParams struct {
	SwitchSprinkling       uint8 `json:"switchSprinkling"`
	SolenoidValveDelay     uint8 `json:"solenoidValveDelay"`
	SprayIntervalTime      uint8 `json:"sprayIntervalTime"`
	FanControl             uint8 `json:"fanControl"`
	LightControl           uint8 `json:"lightControl"`
	ControllerMode         uint8 `json:"controllerMode"`
	SolenoidValveMode      uint8 `json:"solenoidValveMode"`
	AddLightTimePeriod     uint8 `json:"addLightTimePeriod"`
	DeleteLightTimePeriod2 uint8 `json:"deleteLightTimePeriod2"`
	Manual                 uint8 `json:"manual"`
	SecondCurrentThreshold uint8 `json:"secondCurrentThreshold"`
	ParameterId            uint8 `json:"parameterId"`
	ParameterValue         uint8 `json:"parameterValue"`
}

// 设备控制事件上报 method:DEVICE_CONTROL
type DeviceControlEvent struct {
	Method string              `json:"method"`
	Params DeviceControlParams `json:"params"`
}

// 设备控制命令组装并发送给设备
func AssembleDeviceControlPayload(mac string, method string, params map[string]interface{}) error {
	var byteList []ByteData
	// 给params中的数据按照startAddr排序
	for k, v := range params {
		// logrus.Info("k:", k, "v:", v)
		// 是否存在k
		if _, ok := DeviceControlDataMap[k]; ok {
			byteList = append(byteList, ByteData{
				StartAddr: DeviceControlDataMap[k].StartAddr,
				Length:    DeviceControlDataMap[k].Length,
				Value:     v,
			})
		}
	}
	// logrus.Info("未排序的byteList:", byteList)
	err := AssembleCommandPayloadByByteData(mac, method, byteList)
	return err
}
